import { LogServerHelper } from "../../log_server/LogServerHelper";
import { Config } from "../config/Config";
import { DbConfig } from "../config/DbConfig";
import { DaoBase } from "../engine/dao/DaoBase";
import DateUtil from "../engine/utils/DateUtil";
import { Logger } from "../engine/utils/Logger";
import { MysqlClient } from "../engine/utils/MySqlClient";
import { MysqlManager } from "../engine/utils/MysqlManager";
import { ServerManager } from "../server/ServerManager";

export class UserDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_yxx;
    public static Tb_Name: string = "usr";


    private static getDbName() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     return DbConfig.Db_Name.db_yxx2
        // }
        // else if (Config.environment == Config.EnvironmentType.xj) {
        //     return DbConfig.Db_Name.db_yxx2
        // }
        // else if (Config.environment == Config.EnvironmentType.Jk) {
        //     return DbConfig.Db_Name.db_jk
        // }
        // else if(Config.environment == Config.EnvironmentType.chaoyou){
        //     return DbConfig.Db_Name.db_chaoyouhui
        // }
        // else {
            return DbConfig.Db_Name.db_yxx
        // }
    }

    public static async get_user_count() {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = `select count(id) as count from usr`
        let results = await client.queryPromise(sql);
        let count = (results && results.length) ? results[0].count : 0
        return count
    }

    public static async get_today_user_count() {
        let nowDate: Date = new Date()
        let startDate: Date = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate())
        let endDate: Date = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() + 1)
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = `select count(id) as count from usr where createtime >= ? and createtime <= ?`
        let sqlParams = [Math.floor(startDate.getTime() / 1000), Math.floor(endDate.getTime() / 1000)];
        let results = await client.queryPromise(sql, sqlParams);
        let count = (results && results.length) ? results[0].count : 0
        return count
    }


    public static async get_user_list(pagenum: number, pagesize: number) {
        let dbName:string = this.getDbName()
        let client: MysqlClient = await MysqlManager.instance.getDb(dbName);
        let start: number = (pagenum - 1) * pagesize
        let sql: string = "select  usr.id,usr.`name`,usr.nick,usr.face,usr.createtime, usr_resource.gem  from usr  left join usr_resource  on usr.id=usr_resource.id  limit ?,? "
        let sqlParams = [start, pagesize];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("get_user_list=== sql=",dbName,Config.environment, sql, sqlParams)
        return results
    }



    public static async is_user_exist_by_userid(userid: number): Promise<any> {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select userid from " + this.DB_NAME + "." + this.Tb_Name + " where `userid`=?;"
        let sqlParams = [userid];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async get_user_data_by_userid(userid: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select  usr.id,usr.`name`,usr.nick,usr.face,usr.createtime, usr_resource.gem  from usr  left join usr_resource   on usr.id=usr_resource.id  where usr.id=?"
        let sqlParams = [userid];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return results[0]
        }
    }

    public static async get_user_data(account: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let sql: string = "select * from " + this.DB_NAME + "." + this.Tb_Name + " where `account`=?;"
        let sqlParams = [account];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return results[0]
        }
    }


}